package 移除链表元素;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 移除链表元素
 * @date 2022/10/15 11:49
 */
public class Demo203 {

    /**
     * Definition for singly-linked list.
     * */
     public class ListNode {
         int val;
         ListNode next;
         ListNode() {}
         ListNode(int val) { this.val = val; }
         ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     }

    public ListNode removeElements(ListNode head, int val) {
         ListNode pre=new ListNode(-1,head);
         ListNode cur=pre;
         while (cur!=null&&cur.next != null){
             //如果是相等的则跳过元素，可以多次跳跃元素
             while (cur.next != null&&cur.next.val==val){
                 cur.next=cur.next.next;
             }
             //不相等则是移动元素，这里一定要移动
             cur=cur.next;
         }
         return pre.next;
    }
}
